<template>
  <div>
    <el-dialog v-model="dialogVisible" :title="totalName" width="45%" @close="handleDialogClose">
      <el-form
        ref="formRef"
        :inline="true"
        :rules="rules"
        :model="putaddlist"
        @submit.prevent="onSubmit"
      >
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="课程学科" prop="subject">
              <el-select placeholder="" v-model="putaddlist.subject">
                <el-option v-for="item in disciplineIpt" :key="item.id" :value="item.content">{{
                  item.content
                }}</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程名称" prop="name">
              <el-input placeholder="请输入" v-model="putaddlist.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="适应人群" prop="applicablePerson">
              <el-select placeholder="" v-model="putaddlist.applicablePerson">
                <el-option v-for="item in crowdIpt" :key="item.id" :value="item.content">{{
                  item.content
                }}</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程价格" prop="price">
              <el-input placeholder="请输入" v-model="putaddlist.price"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="课程介绍" prop="info">
              <el-input
                v-model="putaddlist.info"
                type="textarea"
                maxlength="20"
                placeholder="请输入"
                rows="3"
                style="width: 265px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" class="btncl">取消</el-button>
          <el-button type="primary" @click="btnOK" class="btnokstyle"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue'
import { addTabelCourse, putTabelCourse } from '@/api/course'
import { ElMessage } from 'element-plus'
const totalName = computed(() => {
  return putaddlist.value.id ? '编辑课程' : '新增课程'
})
const formRef = ref(null)
// 接受父组件数据方法
const prop = defineProps({
  disciplineIpt: Error,
  crowdIpt: Error
})
console.log(prop.list)
// 控制弹窗开关
const dialogVisible = ref(false)
const dialogStatus = () => {
  dialogVisible.value = true
}
const putaddlist = ref({
  name: '',
  applicablePerson: '',
  subject: '',
  price: '',
  info: '',
  id: ''
})
// 表单校验
const rules = ref({
  name: [
    {
      required: true,
      message: '请输入课程名称！',
      trigger: 'blur,change'
    }
  ],
  applicablePerson: [
    {
      required: true,
      message: '请选择适应人群！',
      trigger: 'blur'
    }
  ],
  subject: [
    {
      required: true,
      message: '请输入课程名称！',
      trigger: 'blur'
    }
  ],
  price: [
    {
      required: true,
      message: '请输入课程名称！',
      trigger: 'blur'
    }
  ],
  info: [
    {
      required: true,
      message: '请输入课程名称！',
      trigger: 'blur'
    }
  ]
})
const emit = defineEmits(['add-addList'])
// 暴露
defineExpose({ dialogVisible, dialogStatus, putaddlist })
//添加课程
const btnOK = async () => {
  await formRef.value.validate()
  if (putaddlist.value.id) {
    totalName.value = '编辑课程'
    await putTabelCourse(putaddlist.value)
    await emit('add-addList')
    ElMessage({
      message: '编辑课程成功！',
      type: 'success'
    })
  } else {
    await addTabelCourse(putaddlist.value)
    formRef.value.resetFields()
    await emit('add-addList')
    ElMessage({
      message: '添加课程成功！',
      type: 'success'
    })
  }
  dialogVisible.value = false
}
const handleDialogClose = () => {
  formRef.value.resetFields()
  putaddlist.value = {
    name: '',
    applicablePerson: '',
    subject: '',
    price: '',
    info: '',
    id: ''
  }
}
</script>
<style scoped>
.el-form-item {
  display: block;
}
.btnokstyle {
  width: 94px;
  height: 40px;
  border-radius: 20px;
  background-color: #00cd95;
  color: #fff;
}
.btncl {
  width: 94px;
  height: 40px;
  border-radius: 20px;
  background-color: #eaeaea;
  color: #333;
}
</style>
